package com.scms.secondclass.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.scms.common.annotation.Log;
import com.scms.common.core.controller.BaseController;
import com.scms.common.core.domain.AjaxResult;
import com.scms.common.enums.BusinessType;
import com.scms.secondclass.domain.CreditRecord;
import com.scms.secondclass.service.ICreditRecordService;
import com.scms.common.utils.poi.ExcelUtil;
import com.scms.common.core.page.TableDataInfo;

/**
 * 学分管理Controller
 * 
 * @author scms
 */
@RestController
@RequestMapping("/secondclass/credit")
public class CreditController extends BaseController
{
    @Autowired
    private ICreditRecordService creditRecordService;

    /**
     * 查询学分记录列表
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreditRecord creditRecord)
    {
        startPage();
        List<CreditRecord> list = creditRecordService.selectCreditRecordList(creditRecord);
        return getDataTable(list);
    }

    /**
     * 导出学分记录列表
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:export')")
    @Log(title = "学分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditRecord creditRecord)
    {
        List<CreditRecord> list = creditRecordService.selectCreditRecordList(creditRecord);
        ExcelUtil<CreditRecord> util = new ExcelUtil<CreditRecord>(CreditRecord.class);
        util.exportExcel(response, list, "学分记录数据");
    }

    /**
     * 获取学分记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(creditRecordService.selectCreditRecordByRecordId(recordId));
    }

    /**
     * 新增学分记录
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:add')")
    @Log(title = "学分记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreditRecord creditRecord)
    {
        return toAjax(creditRecordService.insertCreditRecord(creditRecord));
    }

    /**
     * 修改学分记录
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:edit')")
    @Log(title = "学分记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreditRecord creditRecord)
    {
        return toAjax(creditRecordService.updateCreditRecord(creditRecord));
    }

    /**
     * 更新学分记录状态
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:edit')")
    @Log(title = "学分记录状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{recordId}")
    public AjaxResult updateStatus(@PathVariable("recordId") Long recordId, @RequestParam("status") String status)
    {
        CreditRecord creditRecord = new CreditRecord();
        creditRecord.setRecordId(recordId);
        creditRecord.setStatus(status);
        return toAjax(creditRecordService.updateCreditRecord(creditRecord));
    }

    /**
     * 删除学分记录
     */
    @PreAuthorize("@ss.hasPermi('secondclass:credit:remove')")
    @Log(title = "学分记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(creditRecordService.deleteCreditRecordByRecordIds(recordIds));
    }

    /**
     * 获取用户学分统计
     */
    @GetMapping("/stats/{userId}")
    public AjaxResult getUserCreditStats(@PathVariable("userId") Long userId)
    {
        Map<String, Object> stats = creditRecordService.getUserCreditStats(userId);
        return success(stats);
    }

    /**
     * 获取学分统计总览数据
     */
    @GetMapping("/stats/overview")
    public AjaxResult getCreditStatsOverview()
    {
        return success(creditRecordService.getCreditStatsOverview());
    }

    /**
     * 获取当前学期
     */
    @GetMapping("/semester/current")
    public AjaxResult getCurrentSemester()
    {
        // 这里可以从配置或数据库中获取当前学期，这里简单返回一个固定值
        String currentSemester = "2024-2025-2";
        return success(currentSemester);
    }

    /**
     * 获取所有学期列表
     */
    @GetMapping("/semester/list")
    public AjaxResult getSemesterList()
    {
        // 这里可以从数据库中获取所有学期，这里简单返回一些固定值
        List<String> semesters = new ArrayList<>();
        semesters.add("2023-2024-1");
        semesters.add("2023-2024-2");
        semesters.add("2024-2025-1");
        semesters.add("2024-2025-2");
        return success(semesters);
    }
} 